package cn.tedu.tnt_sky.controller.admin;

import cn.tedu.tnt_sky.common.context.BaseContext;
import cn.tedu.tnt_sky.common.response.JwtClaimsConstant;
import cn.tedu.tnt_sky.common.result.Result;
import cn.tedu.tnt_sky.common.utils.JwtUtils;
import cn.tedu.tnt_sky.pojo.dto.AdminLoginDTO;
import cn.tedu.tnt_sky.pojo.dto.AdminUpdateNicknameDTO;
import cn.tedu.tnt_sky.pojo.dto.AdminUpdatePasswordDTO;
import cn.tedu.tnt_sky.pojo.dto.AdminUploadDTO;
import cn.tedu.tnt_sky.pojo.entity.Admin;
import cn.tedu.tnt_sky.pojo.vo.AdminSettingsVO;
import cn.tedu.tnt_sky.pojo.vo.AdminTokenVO;
import cn.tedu.tnt_sky.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/tnt/admin")
@Api(tags = "管理员相关接口")
public class AdminController {

    @Autowired
    AdminService adminService;
    /**
     * 管理员登录
     *
     * @param adminLoginDTO
     * @return
     */
    @ApiOperation(value = "管理员登录")
    @PostMapping("/login")
    public Result login(@RequestBody @Validated AdminLoginDTO adminLoginDTO) {
        log.info("管理登录：{}", adminLoginDTO);
        Admin admin = adminService.AdminLogin(adminLoginDTO);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put(JwtClaimsConstant.ADMIN_ID, admin.getId());
        map.put(JwtClaimsConstant.USERNAME, admin.getUsername());
        map.put(JwtClaimsConstant.PASSWORD, admin.getPassword());

        String token = JwtUtils.createJWT(map);
        AdminTokenVO adminTokenVO = new AdminTokenVO();
        adminTokenVO.setToken(token);
        log.info("管理员token={}", token);
        return Result.success(adminTokenVO);
    }

    @ApiOperation(value = "管理员个人信息修改--昵称")
    @PutMapping("/settingUpdate/nickname")
    public Result settingUpdate(@RequestBody AdminUpdateNicknameDTO adminUpdateNicknameDTO) {
        log.info("管理员个人信息修改:{}", adminUpdateNicknameDTO);
        adminService.updateSettingNickname(adminUpdateNicknameDTO);

        return Result.success();
    }

    @ApiOperation(value = "管理员个人信息修改--密码")
    @PutMapping("/settingUpdate/password")
    public Result settingUpdate(@RequestBody AdminUpdatePasswordDTO adminUpdatePasswordDTO) {
        log.info("管理员个人信息修改:{}", adminUpdatePasswordDTO);
        adminService.updateSettingPassword(adminUpdatePasswordDTO);
        // 销毁token
        return Result.success();
    }

//    /**
//     * 管理员个人信息查询
//     * @param username
//     * @return
//     */
//    @ApiOperation(value = "管理员个人信息查询")
//    @GetMapping("/settingSelect")
//    public Result settingSelect(@RequestParam("username") String username) {
//        log.info("管理员个人信息查询:{}", username);
//        AdminSettingsVO adminSettingsVO = adminService.settingSelect(username);
//        return Result.success(adminSettingsVO);
//    }

    /**
     * 头像上传
     */
    @ApiOperation(value = "头像上传")
    @PostMapping ("/uploadById")
    public Result uploadById(@RequestBody AdminUploadDTO adminUploadDTO){
        log.info("上传:{}", adminUploadDTO);
        adminService.uploadById(adminUploadDTO);
        return Result.success();
    }

    /**
     * 刷新数据
     * @return
     */
    @ApiOperation(value = "刷新数据")
    @GetMapping("/refresh")
    public Result refresh(){
        return Result.success(adminService.refresh());
    }
}
